from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
#from django.contrib.auth import authenticate, login
#from django.contrib import auth

from django.http import HttpResponseRedirect
from django.core.context_processors import csrf

from login.models import client
from login.models import Hotels
from login.models import Review
from login.models import Room
from login.models import Date


# must import  loginForm class
from login.models import loginForm
from login.models import searchForm
from login.models import roomForm
from login.models import reviewForm
from login.models import dateForm

# import query class
from django.db.models import Q
from django.db import connection


##############################################################################################################


def index(request):


    return render(request,'login/index.html')




def search_hotel(request):

    hotel_info = Hotels.objects.all()

    # must in a dictionary
    hotel_data ={'hotel_detail':hotel_info}
    # to dispaly data


    return render(request,'login/search_hotel.html',hotel_data)


def gallery(request):

    hotel_id = request.GET['hotel_id']


    # must in a dictionary
    hotel_data_id = {'hotel_id':hotel_id}


    return render(request,'login/gallery.html',hotel_data_id)




def reviews(request):
    #from from url
    hotel_id = request.GET['hotel_id']


    hotel_info = Review.objects.filter(myreview_id_id = hotel_id)

    # must in a dictionary
    hotel_data ={'hotel_detail':hotel_info,'hotel_id':hotel_id}

    # to dispaly data
    # using render to repaint html page

    return render(request,'login/reviews.html',hotel_data)



def new_review(request):

    # Check if the session has a given key:
    if "username" in request.session:
        #from from url
        hotel_id = request.GET['hotel_id']

        obj_form = reviewForm()

        context = {'obj_form': obj_form,'hotel_id':hotel_id}

        return render(request,'login/new_review.html',context)

    else:
        return HttpResponseRedirect('/accounts/login/')


def add_review(request):

    if request.method == 'POST':

        user_review=request.POST['myreview']
        myreview_date_post = request.POST['myreview_date']
        myreview_id_post = request.POST['myreview_id']

        # Check if the session has a given key:
        if "username" in request.session:

            # Get a session value -- this could be called in a different view,
            # or many requests later (or both):
            username = request.session["username"]

            form = Review(myreview=user_review,
                myreview_user=username,
                myreview_date=myreview_date_post,
                myreview_id_id=myreview_id_post
            )

            form.save()

            return HttpResponseRedirect('/login/booking')
        else:
            return HttpResponseRedirect('/login/')

############# to show form on browser
    args = {}
    args.update(csrf(request))

    args['form'] = reviewForm()
    print args

    return render(request,'/login/new_review.html',args)

##################################################################################################

def booking(request):

    global obj_form2
    global obj_form3

    hotel_info = Hotels.objects.all()

    # must in a dictionary
    # to send to form name of hotels & country
    hotel_data ={'hotel_info':hotel_info}

    obj_form = searchForm()
    obj_form2= roomForm()
    obj_form3=dateForm()

    context = {'obj_form': obj_form,'obj_form2':obj_form2,'obj_form3':obj_form3,'hotel_info':hotel_info}

    return render(request,'login/booking.html',context)

###################################################################################################
def check_ava(request):

    if request.method == 'POST':

        hotel_name_post = request.POST['hotel_name']
        country_post = request.POST['country']
        room_type_post = request.POST['room_type']

        busy_start_post = request.POST['busy_start']
        busy_end_post = request.POST['busy_end']


        hotel_info = Hotels.objects.filter(

            Q(hotel_name=hotel_name_post) &
            Q(country=country_post)
            )
        # must in a dictionary
        #hotel_data ={'hotel_info':hotel_info}

        if not hotel_info:
            return render(request,'login/no_result.html')



##############################################################################################
        else:

            date_info = Date.objects.filter(


                Q(busy_start=busy_start_post) & Q(busy_end=busy_end_post) &

                Q(busy_start__range=(busy_start_post,busy_end_post)) &

                Q(busy_end__range=(busy_start_post,busy_end_post))

                )

            #if not date_info:

            room_info =Room.objects.filter(
                    #forign key
                    Q(hotel_id_id=hotel_info[0].id)&

                    Q(room_type=room_type_post)
                    )


            if not room_info:
                    return render(request,'login/no_result.html')
            else:

                # must in a dictionary
                room_data ={'room_info':room_info,'hotel_info':hotel_info,
                                'date_info':date_info,

                                'busy_start':busy_start_post,'busy_end':busy_end_post}

                return render(request,'login/list_rooms.html',room_data)
                    #return render(request,'login/list_rooms.html',room_data)
            #else:
                #return render(request,'login/no_result.html')

#############################################################################################################
def make_reservation(request):

    # Check if the session has a given key:
    if "username" in request.session:
        username = request.session["username"]

        if request.method == 'POST':


            room_id_id_post=request.POST['room_id_id']
            busy_start_post = request.POST['busy_start']
            busy_end_post = request.POST['busy_end']

            # to insert into DB Room(is name of table & there fields)


            obj_form2 = Date(room_id_id=room_id_id_post,

                    reserve_user=username,

                    busy_start=busy_start_post,
                    busy_end=busy_end_post
                    )

            obj_form2.save()

            return render(request,'login/make_reservation.html')

    else:
        return HttpResponseRedirect('/accounts/login/')


#############################################################################################################
def no_result(request):

    return render(request,'login/no_result.html')


###################################################################################################################

